home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / include / ooglutil.h < prev    next >
C/C++ Source or Header  |  1993-10-10  |  10KB  |  275 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #ifndef OOGLUTILDEF
  13. #define OOGLUTILDEF
  14.  
  15. #include <stdio.h>
  16.  
  17. /*
  18.  * Definition so that SGIs won't barf on member function prototypes.
  19.  */
  20. #ifdef sgi
  21. #define P(foo) ()
  22. #else
  23. #define P(foo) foo
  24. #endif
  25.  
  26.  
  27. #define    COUNT(array)    (sizeof(array)/sizeof(array[0]))
  28.  
  29. /*
  30.  * Public definitions for miscellaneous useful OOGL internal stuff.
  31.  */
  32.  
  33. #define    OOGLMagic(key, vers)    (0x9c800000 | (((key)&0x7f)<<16) | ((vers)&0xffff) )
  34. #define OOGLIsMagic(magic)    (((magic) & 0xff800000) == 0x9c800000)
  35.  
  36. /*
  37.  * Memory allocation
  38.  */
  39. extern void *(*OOG_NewP)(int);
  40. extern void *(*OOG_RenewP)(void *, int);
  41. extern void *OOG_NewE(int, char *);
  42. extern void *OOG_RenewE(void *, int, char *);
  43.  
  44. #define    OOGLNew(t)        (t *)(*OOG_NewP)(sizeof(t))
  45. #define    OOGLNewN(t,N)        (t *)(*OOG_NewP)(sizeof(t)*(N))
  46. extern void (*OOGLFree)(void *);
  47. #define    OOGLRenewN(t,p,N)    (t *)(*OOG_RenewP)(p, sizeof(t)*(N))
  48. #define    OOGLRealloc(t,p)    (t *)(*OOG_RenewP)(p, sizeof(t))
  49.  
  50. #define    OOGLNewE(t, errmsg)    (t *)OOG_NewE(sizeof(t), errmsg)
  51. #define    OOGLNewNE(t,N, errmsg)    (t *)OOG_NewE(sizeof(t)*(N), errmsg)
  52. #define    OOGLRenewNE(t,p,N, errmsg) (t *)OOG_RenewE(p, sizeof(t)*(N), errmsg)
  53.  
  54. /*
  55.  * Backward compatibility
  56.  */
  57. #define    GeomNew(t)        OOGLNew(t)
  58. #define    GeomNewN(t,N)        OOGLNewN(t,N)
  59. #define    GeomFree(p)        OOGLFree(p)
  60. #define    GeomError          OOGLError
  61.  
  62. /*
  63.  * Variable-sized arrays ("vectors").  Manipulates variables of type "vvec".
  64.  * Maintains the data they point to, but doesn't allocate the vvec's themselves;
  65.  * typical use might be
  66.  *   vvec myvec;
  67.  *   VVINIT(myvec, float, 10);
  68.  *   while( ... ) {
  69.  *    *VVAPPEND(myvec, float) = something;
  70.  *   }
  71.  *   for(i = 0; i < VVSIZE(myvec); i++)
  72.  *    ... VVEC(myvec, float)[i] ...
  73.  * 
  74.  */
  75. typedef struct vvec {
  76.     char *base;    /* The real data */
  77.     int count;    /* Number of elements in use (indices 0..count-1) */
  78.     int allocated;    /* Number of elements allocated */
  79.     int elsize;    /* sizeof(element type) */
  80.     char dozero;    /* Zero-fill all new elements */
  81.     char malloced;    /* "base" has been malloced */
  82.     char spare1, spare2; /* for future extensions */
  83. } vvec;
  84.  
  85. /*
  86.  * Macros take 'vvec' arguments, while functions take addresses of vvec's.
  87.  * VVINIT() : initialize new empty array; first malloc will grab >= 'minelems'.
  88.  * VVZERO() : request that all newly allocated elements be cleared to zeros
  89.  * VVINDEX() : return address of index'th element, ensuring that it exists.
  90.  * VVAPPEND() : increments array size, returning address of new last element
  91.  * VVEC()   : returns address of array base; VVEC()[i] is the i'th element
  92.  *        assuming that its existence was ensured by earlier call to
  93.  *        VVAPPEND() or VVINDEX() or vvneeds().
  94.  * VVCOUNT() : number of valid elements in array.  Maintained by user.
  95.  */
  96. #define    VVINIT(vv,type,minelems)  vvinit(&(vv), sizeof(type), minelems)
  97. #define    VVEC(vv, type)        ((type *)((vv).base))
  98. #define    VVCOUNT(vv)        (vv).count
  99. #define    VVINDEX(vv,type,index)    ((type *)vvindex(&(vv), (index)))
  100. #define    VVAPPEND(vv, type)      VVINDEX(vv, type, (vv).count++)
  101.  
  102. /* Functions take addresses of vvec's.  Besides the above, there are
  103.  * vvtrim() : trim allocated but unused space (beyond VVCOUNT()'th element).
  104.  * vvfree() : free malloced array.
  105.  * vvneeds(): ensure that at least this many elements are allocated
  106.  * vvzero() : all newly allocated elements will be filled with binary zeros
  107.  * vvuse()  : Use the given non-malloced buffer until more room is needed;
  108.  *        call vvuse() after vvinit() but before allocating anything.
  109.  * vvcopy() : copies one vvec into another, allocating space in the 
  110.  *              destination to accommodate the data and copying everything.
  111.  */
  112. extern void vvinit(vvec *v, int elsize, int minelems);
  113. extern void vvuse(vvec *v, void *buf, int allocated);
  114. extern void vvtrim(vvec *v);        /* Trim allocated but unused data */
  115. extern void vvfree(vvec *v);        /* Free all malloced data */
  116. extern void vvneeds(register vvec *v, int needed);
  117. extern void *vvindex(vvec *v, int index);
  118. extern void vvzero(vvec *v);
  119. extern void vvcopy(vvec *src, vvec *dest);
  120.  
  121.  
  122.  
  123. /*
  124.  * Error handling
  125.  */
  126. extern char *_GFILE;        /* Name of file where error is found */
  127. extern int _GLINE;        /* Line number in file where error is found */
  128. extern int OOGL_Errorcode;    /* Unique integer error code */
  129. extern void GeomWarn (char *fmt, ...);
  130. extern char *sperrno(unsigned int);
  131. extern char *sperror(void);
  132.  
  133. /* Kludge for obtaining file name and line number of error: */
  134. #define OOGLError (_GFILE= __FILE__, _GLINE=__LINE__,0)?0:_OOGLError
  135.  
  136. extern int _OOGLError(int, char *fmt, ...);
  137.  
  138. extern void OOGLSyntax(FILE *, char *fmt, ...); 
  139.  
  140.     /* Bit fields in error codes */
  141. #define    OE_VERBOSE    0x1
  142. #define    OE_FATAL    0x2
  143.  
  144.  
  145. /*
  146.  * File-I/O utility routines
  147.  */
  148. extern int fnextc(FILE *, int flags);
  149. extern int fexpectstr(FILE *, char *string);
  150. extern int fexpecttoken(FILE *, char *string);
  151. extern char *ftoken(FILE *, int fnextc_flags);
  152. extern char *fdelimtok(char *delims, FILE *f, int flags);
  153.  
  154. extern int fgetnf(FILE *, int nfloats, float *floatp, int binary);
  155. extern int fputnf(FILE *, int nfloats, float *floatp, int binary);
  156. extern int fgetni(FILE *, int nints, int *intp, int binary);
  157. extern int fgetns(FILE *, int nshorts, short *shortp, int binary);
  158. extern int fgettransform(FILE *, int ntrans, float *transforms, int binary);
  159. extern int fputtransform(FILE *, int ntrans, float *transforms, int binary);
  160. extern FILE *fstropen(char *buf, int buflen, char *mode);
  161.  
  162. /*
  163.  * fcontext(f) returns a string indicating the current position of file f,
  164.  * or the empty string if no data are available.
  165.  */
  166. extern char *fcontext(FILE *f);
  167.  
  168.  
  169. #define    NODATA    -2    /* async_fnextc() and async_getc() return NODATA if
  170.              * none is immediately available
  171.              */
  172.  
  173. extern int async_fnextc(FILE *, int flags);
  174. extern int async_getc(FILE *);
  175.  
  176. extern struct stdio_mark *stdio_setmark(struct stdio_mark *, FILE *);
  177. extern int          stdio_seekmark(struct stdio_mark *);
  178.  
  179. /*
  180.  * int fnextc(FILE *f, int flags)
  181.  *    Advances f to the next "interesting" character and
  182.  *    returns it.  The returned char is ungetc'ed so the next getc()
  183.  *    will yield the same value.
  184.  *    Interesting depends on flags:
  185.  *      0 : Skip blanks, tabs, newlines, and comments (#...\n).
  186.  *      1 : Skip blanks, tabs, and comments, but newlines are interesting
  187.  *        (including the \n that terminates a comment).
  188.  *      2 : Skip blanks, tabs, and newlines, but stop at #.
  189.  *      3 : Skip blanks and tabs but stop at # or \n.
  190.  *
  191.  * int
  192.  * fexpectstr(FILE *file, char *string)
  193.  *    Expect the given string to appear immediately on file.
  194.  *    Return 0 if the complete string is found,
  195.  *    else the offset+1 of the last matched char within string.
  196.  *    The first unmatched char is ungetc'd.
  197.  *
  198.  * int
  199.  * fexpecttoken(FILE *file, char *string)
  200.  *    Expect the given string to appear on the file, possibly after
  201.  *    skipping some white space and comments.
  202.  *    Return 0 if found, else the offset+1 of last matched char in string.
  203.  *    The first unmatched char is ungetc'd.
  204.  *
  205.  * char *ftoken(FILE *f, int flags)
  206.  *    Skips uninteresting characters with fnextc(f, flags),
  207.  *    then returns a "token" - string of consecutive interesting characters.
  208.  *    Returns NULL if EOF is reached with no token, or if
  209.  *    flags specifies stopping at end-of-line and this is encountered with
  210.  *    no token found.
  211.  *    The token is effectively statically allocated and will be
  212.  *    overwritten by the next ftoken() call.
  213.  */
  214. /*
  215.  * int fgetnf(file, nfloats, floatp, binary)
  216.  *    Read an array of floats from a file in "ascii" or "binary" format.
  217.  *    Returns number of floats successfully read, should = nfloats.
  218.  *    "Binary" means "IEEE 32-bit floating-point" format.
  219.  *
  220.  * int fgetni(FILE *file, int nints, int *intsp, int binary)
  221.  *    Read an array of ints from a file in "ascii" or "binary" format.
  222.  *    Returns number of ints successfully read, should = nints.
  223.  *    "Binary" means "32-bit big-endian" integer format.
  224.  *
  225.  * int fgetns(FILE *file, int nshorts, short *intsp, int binary)
  226.  *    Read an array of shorts from a file in "ascii" or "binary" format.
  227.  *    Returns number of shorts successfully read, should = nints.
  228.  *    "Binary" means "16-bit big-endian" integer format.
  229.  *
  230.  * int fgettransform(FILE *f, int ntransforms, float *transforms, int binary)
  231.  *    Reads 4x4 matrices from FILE.  Returns the number of matrices found,
  232.  *    up to ntransforms.  Returns 0 if no numbers are found.
  233.  *    On finding incomplete matrices (not a multiple of 16 floats)
  234.  *    returns -1, regardless of whether any whole matrices were found.
  235.  *    In ASCII (binary==0) mode, matrices are read in conventional order
  236.  *    which is the transpose of the internal form.  Binary mode reads
  237.  *    32-bit IEEE floats in internal order.
  238.  *
  239.  * int fputtransform(FILE *f, int ntransforms, float *transforms, int binary)
  240.  *    Writes 4x4 matrices to FILE.  Returns the number written, i.e.
  241.  *    ntransforms unless an error occurs.  See fgettransform() for format.
  242.  *
  243.  * FILE *fstropen(str, len, mode)
  244.  *    Opens a string (buffer) as a "file".
  245.  *    Mode is the usual "r", "w", etc. stuff.
  246.  *    Reads should return EOF on encountering end-of-string,
  247.  *    writes past end-of-string should also yield an error return.
  248.  *    fclose() should be used to free the FILE after use.
  249.  */
  250.  
  251. extern char *findfile(char *superfile, char *file);
  252. extern void filedirs(char *dirs[]);
  253. extern char **getfiledirs();
  254. char *envexpand(char *s);
  255.  
  256. extern char **ooglglob(char *s);
  257. extern ooglblkfree(char **av0);
  258.  
  259.  
  260. /*
  261.  * macro for grabbing the next value from either a region of
  262.  * memory or a va_list.
  263.  * al should be the address of the va_list
  264.  * p should be the address of a poiner to the region of memory,
  265.  *   or NULL, which means use the va_list instead.
  266.  */
  267. #define OOGL_VA_ARG(type,al,p) (p ? ((type*)((*p)+=sizeof(type)))[-1] \
  268.     : (va_arg (*al, type)))
  269.  
  270.  
  271. #include "porting.h"
  272.  
  273.  
  274. #endif
  275.